package com.hulk.ratel.common.util;

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * @author hulk
 * @E-mail: 29572320@qq.com
 * @Date: Create in 2019-05-10 23:46
 * @Version: 1.0.0
 * @Modify by:
 * Class description
 */
@Slf4j
public class OptionalUtil {

	/**
	 * 判断输入的字符串是否为空
	 * @param str String
	 * @return
	 */
	public static boolean isEmpty(String str){
		return  Optional.ofNullable(str).isPresent();
	}

	/**
	 * 判断输入的Integer是否为空
	 * @param num Integer
	 * @return
	 */
	public static boolean isEmpty(Integer num){
		return  Optional.ofNullable(num).isPresent();
	}
	/**
	 * 判断输入的Double是否为空
	 * @param dnum Double
	 * @return
	 */
	public static boolean isEmpty(Double dnum){
		return  Optional.ofNullable(dnum).isPresent();
	}
	/**
	 * 判断输入的Object是否为空
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj){
		return  Optional.ofNullable(obj).isPresent();

	}


	/**
	 * 防止取值NPE方法
	 */
	public static <T> Optional<T> get(Supplier<T> supplier) {
		T result = supplier.get();
		return Optional.ofNullable(result);

	}

	public static Optional<Short> str2Short(String s) {
		try {
			return Optional.of(Short.parseShort(s));
		} catch (NumberFormatException e) {
			log.error("", e);
			return Optional.empty();
		}
	}

	public static Optional<Integer> str2Int(String s) {
		try {
			return Optional.of(Integer.parseInt(s));
		} catch (NumberFormatException e) {
			log.error("", e);
			return Optional.empty();
		}
	}

	public static Optional<Long> str2Long(String s) {

		try {
			return Optional.of(Long.parseLong(s));
		} catch (NumberFormatException e) {
			log.error("", e);
			return Optional.empty();
		}

	}
}
